<?php
include_once 'common.inc.php';

if($operation == 'display'){
    $poster_sum = pdo_fetchcolumn("select count(*) from ".tablename(MODULE_PREFIX."poster")." where uniacid = $uniacid ");
    $my_poster_sum = pdo_fetchcolumn("select count(*) from ".tablename(MODULE_PREFIX."qr")." where uniacid = $uniacid and uid = $uid ");
}

elseif ($operation == 'build'){
    $content = trim(urldecode($_GPC["content"]));
    
    $poster = pdo_fetch("select * from " . tablename(MODULE_PREFIX.'poster') . " where keyword='".$content."' and uniacid = $uniacid limit 1");
    
    if (empty($poster)) {
        exit;
    }
    
    if(empty($poster['isopen'])){
        $opentext = $poster['opentext'];
        if(empty($opentext)){
            $opentext = "当前海报未开放！";
        } 
        if(!empty($poster['openurl'])){
            $opentext = "<a href='".$poster['openurl']."'>".$opentext."</a>";
        }
        sendCustomNotice($openid, $opentext);
        exit;
    }
    
    $qr = getQR($poster, $openid);

    if (is_error($qr)) {
        sendCustomNotice($openid, "生成二维码出错: " . $qr["message"]);
        exit;
    }
    
    $result  = createPoster($poster, $member, $qr , true, $xcx);
    $mediaid = $result["mediaid"];
    if($xcx == 1){
		echo $result['img'];
	}else{
		if (!empty($mediaid)) {
			sendImage($openid, $mediaid);
		} else {
			$oktext = "<a href='" . $result['img'] . "'>点击查看您的邀请海报</a>";
			sendCustomNotice($openid, $oktext);
		}
		$oktext = !empty($poster["oktext"]) ? $poster["oktext"] : "邀请海报已生成！";
		sendCustomNotice($openid, $oktext);
	}
    
    exit;
}

include $this->template('index');